#include "../../HeadFiles/StandardHeads.h"
#include "../../HeadFiles/StrSimilar.h"
#include "../../HeadFiles/InputCheck.h"
// import getch
#include <conio.h>

// 用extern声明，直接使用main.c里的这三个指针
// 在Chain相关函数里，用对指针的指针来实现操作main.c内这些数据的方法，实际上不如这种方法
// 之后会考虑重构Chains相关函数，统一为extern形式

extern struct RecordNode * recordHead, * recordTail, * recordDisplayPtr;
extern struct CommodityNode * commodityHead, * commodityTail;
// 这种测试函数不放在头文件里，本文件内部使用就行了
// 类似于class里的private函数
void checkHeadAndTail()
{
    if(recordHead != NULL)
        printf("head = %d\n", recordHead->unitCount);
    else
        puts("NO head!!!");
    if(recordTail != NULL)
        printf("tail = %d\n", recordTail->unitCount);
    else
        puts("NO tail!!!");
}

int RecordPage()
{
    system("cls");
    puts("Now displaying sales record:");
    printf("type 1 for the previous\n");
    printf("type 2 for the next\n");
    printf("type 3 to delete this one\n");
    printf("type 4 to add one\n");
    printf("type 5 to search in commodity/client name\n");
    printf("type 0 to get back\n");
    // checkHeadAndTail();
    puts(SPLIT_STR);


    if(recordDisplayPtr != NULL)
        displayThisRecord(recordDisplayPtr);   // 先展示最新的一条
    else{
        puts("No record in database! \ntype 1 to add one or 0 to get back!");
        char opt = getche();
        while(!(opt == '1' || opt == '0'))
        {
            puts("Illegal input detected!Try again:");
            opt = getche();
        }
        if(opt == '1')
        {
            goto Add_Record_Page;
        }else
            return 0;
    }

    char opt2 = getche();
    while(!(opt2 == '1' || opt2 == '2' || opt2 == '3' || opt2 == '4' || opt2 == '5' || opt2 == '0'))
    {
        puts("Illegal input detected! Try again:");
        opt2 = getche();
    }

    // opt2已经合法
    if(opt2 == '2')
    {
        // 查询下一条记录
        if(recordDisplayPtr->nextRecord != NULL)
        {
            recordDisplayPtr = recordDisplayPtr->nextRecord;
            displayThisRecord(recordDisplayPtr);
            // goto Record_Page;
            return 1;
        }else{
            puts("No next record! Getting back to the previous page!");
            system("pause");
            // goto Record_Page;
            return 1;
        }
    }else if(opt2 == '1'){
        // 查询上一条记录
        if(recordDisplayPtr->preRecord != NULL)
        {
            recordDisplayPtr = recordDisplayPtr->preRecord;
            displayThisRecord(recordDisplayPtr);
            // goto Record_Page;
            return 1;
        }else{
            puts("No previous record! Getting back to the previous page!");
            system("pause");
            // goto Record_Page;
            return 1;
        }
    }else if(opt2 == '3'){
        // 删除这一条
        // tempPtr存储displayPtr删除之后应该指向哪里
        struct RecordNode * tempPtr = NULL;
        if(recordDisplayPtr->preRecord == NULL && recordDisplayPtr->nextRecord == NULL)
        {
            tempPtr = NULL;   // 最后一个节点被删除了
        }else{
            // 如果不是最后一个，则去往任意一个不为空的节点即可
            if(recordDisplayPtr->nextRecord != NULL)
                tempPtr = recordDisplayPtr->nextRecord;
            else
                tempPtr = recordDisplayPtr->preRecord;
        }
        delRecord(&recordHead, &recordTail, recordDisplayPtr);
        recordDisplayPtr = tempPtr;
        // goto Record_Page;
        rebuildRecordFile(recordHead);
        return 1;
    }else if(opt2 == '4'){
        // 新增一条
Add_Record_Page:
        addRecord(&recordHead, &recordTail);
        rebuildRecordFile(recordHead);
        recordDisplayPtr = recordTail;
        // goto Record_Page;
        return 1;
    }else if(opt2 == '5'){
        // 模糊查询
        system("cls");
        puts("Please input key word: ");
        char keyWord[STR_MAX_LEN];
        scanf("%s", keyWord);
        strToLower(keyWord);
        struct RecordNode * searchPtr = recordHead;
        while(searchPtr != NULL)
        {
            char tempClientName[STR_MAX_LEN];
            memset(tempClientName, 0, sizeof tempClientName);
            memcpy(tempClientName, searchPtr->clientName, sizeof(searchPtr->clientName));
            strToLower(tempClientName);

            char tempCommodityName[STR_MAX_LEN];
            memset(tempCommodityName, 0, sizeof tempCommodityName);
            memcpy(tempCommodityName, searchPtr->commodityName, sizeof(searchPtr->commodityName));
            strToLower(tempCommodityName);

            double f1 = getStrSimilarity(keyWord, tempCommodityName);
            double f2 = getStrSimilarity(keyWord, tempClientName);
            if(f1 >= 0.7 || f2 >= 0.7)
            {
                displayThisRecord(searchPtr);
            }
            searchPtr = searchPtr->nextRecord;
        }
        puts(SPLIT_STR);
        puts("Display finished!");
        system("pause");
        return 1;
    }else{
        // 返回上一页
        // goto Welcome_Page;
        return 0;
    }
}




